/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.enums.AbnormalEnum;
import com.pig4cloud.pigx.common.customize.enums.CommonDictEnum;
import com.pig4cloud.pigx.common.customize.exception.DuckweedException;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.user.dto.CurrencyPageDTO;
import com.pig4cloud.pigx.user.entity.Currency;
import com.pig4cloud.pigx.user.service.CurrencyService;
import com.pig4cloud.pigx.user.vo.AppCurrencyVO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 币种
 *
 * @author pigx code generator
 * @date 2022-11-21 15:50:12
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/currency" )
@Api(value = "currency", tags = "币种管理")
public class CurrencyController {

    private final  CurrencyService currencyService;

	private final RedisTemplate redis;

    /**
     * 分页查询
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/page" )
    public R<Page<Currency>> getCurrencyPage(@RequestBody CurrencyPageDTO dto) {
		try {
			Page<Currency> page = currencyService.page(new Page<Currency>() {{
				setCurrent(dto.getCurrent());
				setSize(dto.getSize());
			}}, new QueryWrapper<Currency>() {{
				if (dto.getName() != null) {
					eq("name", dto.getName());
				}
				orderByDesc("create_time");
			}});
			for (Currency record : page.getRecords()) {
				record.setState(CommonDictEnum.Status.findName(record.getState()));
				record.setServiceChargePrivateKey(null);
			}
			return R.ok(page);
		} catch (Exception e) {
			throw new DuckweedException(AbnormalEnum.MSG.DISCONNECTED);
		}
    }

	/**
	 * app 获取币种信息
	 * @param currency 币种
	 * @return r
	 */
	@GetMapping("/detailed")
	public R<AppCurrencyVO> getAppCurrency(@RequestParam("currency") String currency,@RequestParam("chain") String chain) {
		return currencyService.getAppCurrency(currency,chain);
	}

	@GetMapping("/{id}")
	public R<Currency> getAdminCurrency(@PathVariable Long id) {
		Currency currency = new Currency().selectById(id);
		currency.setServiceChargePrivateKey(null);
		return R.ok(currency);
	}

    /**
     * 新增币种
     * @param currency 币种
     * @return R
     */
    @ApiOperation(value = "新增币种", notes = "新增币种")
    @SysLog("新增币种" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('user_currency_add')" )
    public R save(@RequestBody Currency currency) {
        return R.ok(currencyService.saveCurrency(currency));
    }

    /**
     * 修改币种
     * @param currency 币种
     * @return R
     */
    @ApiOperation(value = "修改币种", notes = "修改币种")
    @SysLog("修改币种" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('user_currency_edit')" )
    public R updateById(@RequestBody Currency currency) {
		currencyService.updateCurrency(currency);
		return R.ok();
    }

    /**
     * 通过id删除币种
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除币种", notes = "通过id删除币种")
    @SysLog("通过id删除币种" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_currency_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(currencyService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param currency 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('user_currency_export')" )
    public List<Currency> export(Currency currency) {
        return currencyService.list(Wrappers.query(currency));
    }
}
